﻿namespace YunQue.Services.System
{
    /// <summary>
    /// 登录接口
    /// </summary>
    public class LoginService : YunQueBaseService, IScoped
    {
        private readonly UserService _userService;
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="serviceProvider"></param>
        /// <param name="userService"></param>
        public LoginService(IServiceProvider serviceProvider, UserService userService) : base(serviceProvider)
        {
            _userService = userService;
        }

        /// <summary>
        /// 登录时获取用户信息
        /// </summary>
        /// <param name="name"></param>
        /// <param name="password"></param>
        /// <returns></returns>
        /// <exception cref="Exception"></exception>
        public async Task<UserInfo> GetUserWithLoginAsync(string name,string password)
        {
            name = RASEncryption.Decrypt(name);
            //前端传过来的密码是经过RSA加密的，这里需要解密
            //保存到数据库的密码是经过AES加密的，然后需要加密
            password = RASEncryption.Decrypt(password);
            password = AesEncryption.Encrypt(password);
            return await _userService.GetUserByPassword(name, password).ConfigureAwait(false);
        }
    }
}
